package com.zhp.dao;

import com.zhp.dao.base.BaseDao;
import com.zhp.model.Dept;
import com.zhp.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DeptDao extends BaseDao<Dept> {
    public DeptDao() {
        super(Dept.class);
    }


    public List<Dept> findAll() {
        String sql = "select * from dept";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        List<Dept> deptList = new ArrayList<>();
        try {
            deptList = queryRunner.query(sql, new BeanListHandler<>(Dept.class));

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deptList;
    }

    public List<Map<String, Object>> findUserList() {
        String sql = "select d.id as dept_id,d.`name` as dept_name,ui.user_id,ui.real_name\n" +
                "from dept d\n" +
                "left join user u on u.dept_id = d.id\n" +
                "left join user_info ui on ui.user_id = u.id";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> deptList = new ArrayList<>();
        try {
            deptList = queryRunner.query(sql, new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deptList;
    }
}
